package com.frcdi.qms.system.service.impl;

import java.util.List;


import com.frcdi.qms.common.enums.BusinessType;
import com.frcdi.qms.common.utils.StringUtils;
import com.frcdi.qms.system.domain.SysUserOperLog;
import com.frcdi.qms.system.mapper.SysUserOperLogMapper;
import com.frcdi.qms.utils.LogManage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.frcdi.qms.system.domain.SysNotice;
import com.frcdi.qms.system.mapper.SysNoticeMapper;
import com.frcdi.qms.system.service.ISysNoticeService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 公告 服务层实现
 *
 * @author cell
 */
@Service
public class SysNoticeServiceImpl implements ISysNoticeService
{
    @Autowired
    private SysNoticeMapper noticeMapper;

    @Autowired
    private SysUserOperLogMapper operLogMapper;

    /**
     * 查询公告信息
     *
     * @param noticeId 公告ID
     * @return 公告信息
     */
    @Override
    public SysNotice selectNoticeById(Long noticeId)
    {
        return noticeMapper.selectNoticeById(noticeId);
    }

    /**
     * 查询公告列表
     *
     * @param notice 公告信息
     * @return 公告集合
     */
    @Override
    public List<SysNotice> selectNoticeList(SysNotice notice)
    {
        return noticeMapper.selectNoticeList(notice);
    }

    /**
     * 新增公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertNotice(SysNotice notice)
    {
        int row = noticeMapper.insertNotice(notice);

        // 日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "通知公告", BusinessType.INSERT.ordinal());
        operLog.setOperAfter(StringUtils.getFieldAnnotation(selectNoticeById(notice.getNoticeId())));
        operLogMapper.insertSysUserOperLog(operLog);
        return row;
    }

    /**
     * 修改公告
     *
     * @param notice 公告信息
     * @return 结果
     */
    @Override
    @Transactional
    public int updateNotice(SysNotice notice)
    {
        SysNotice oldresult = selectNoticeById(notice.getNoticeId());
        int row = noticeMapper.updateNotice(notice);

        // 日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "通知公告", BusinessType.UPDATE.ordinal());
        operLog.setOperBefore(StringUtils.getFieldAnnotation(oldresult));
        operLog.setOperAfter(StringUtils.getFieldAnnotation(selectNoticeById(notice.getNoticeId())));
        operLogMapper.insertSysUserOperLog(operLog);

        return row;
    }

    /**
     * 删除公告对象
     *
     * @param noticeId 公告ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNoticeById(Long noticeId)
    {
        SysNotice oldresult = selectNoticeById(noticeId);
        int row = noticeMapper.deleteNoticeById(noticeId);
        //日志
        SysUserOperLog operLog = LogManage.getLog("系统管理", "通知公告", BusinessType.DELETE.ordinal());
        operLog.setOperBefore(StringUtils.getFieldAnnotation(oldresult));
        operLogMapper.insertSysUserOperLog(operLog);
        return row;
    }

    /**
     * 批量删除公告信息
     *
     * @param noticeIds 需要删除的公告ID
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteNoticeByIds(Long[] noticeIds)
    {
        StringBuilder result = new StringBuilder();
        for (Long noticeId : noticeIds) {
            SysNotice notice = selectNoticeById(noticeId);
            result.append(StringUtils.getFieldAnnotation(notice));
            result.append(";");
        }
        //日志记录
        SysUserOperLog operLog = LogManage.getLog("系统管理", "通知公告", BusinessType.DELETE.ordinal());
        operLog.setOperBefore(String.valueOf(result.deleteCharAt(result.length()-1)));
        operLogMapper.insertSysUserOperLog(operLog);

        return noticeMapper.deleteNoticeByIds(noticeIds);
    }
}
